package com.streamkar.entity.base;

import com.streamkar.constant.Constants;
import org.springframework.beans.BeanUtils;

public class RequestPaging extends Request {

    /**
     *
     */
    private static final long serialVersionUID = 7584671848291002123L;

    private static final String ASC = "0";

    private Integer page;

    private Integer pageSize;

    private String sortBy = "1";

    private String sortType = "1";

    private String startTime;

    private String endTime;

    public <T extends PagingEntity> T toEntity(Class<T> clazz) throws InstantiationException, IllegalAccessException {
        T entity = clazz.newInstance();
        BeanUtils.copyProperties(this, entity);
        entity.setLimit(this.getLimit());
        entity.setOffset(this.getOffset());
        entity.setOrderBy(this.getSortBy());
        entity.setOrderByType(this.getSortType());
        return entity;
    }

    public Integer getPage() {
        if (page == null) {
            return new Integer(1);
        }
        return page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    public Integer getPageSize() {
        if (pageSize == null) {
            return new Integer(30);
        } else if (pageSize.intValue() > 1000) {
            return new Integer(Constants.PAGING_MAX);
        }
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getOffset() {
        Integer page = getPage();
        Integer pageSize = getPageSize();
        Integer offset = (page - 1) * pageSize;
        return offset;
    }

    public Integer getLimit() {
        return getPageSize();
    }

    public String getSortBy() {
        return sortBy;
    }

    public void setSortBy(String sortBy) {
        this.sortBy = sortBy;
    }

    public String getSortType() {
        if (ASC.equalsIgnoreCase(sortType)) {
            sortType = "asc";
        } else {
            sortType = "desc";
        }
        return sortType;
    }

    public void setSortType(String sortType) {
        this.sortType = sortType;
    }

    public String getStartTime() {
        return startTime;
    }

    public void setStartTime(String startTime) {
        this.startTime = startTime;
    }

    public String getEndTime() {
        return endTime;
    }

    public void setEndTime(String endTime) {
        this.endTime = endTime;
    }
}

